package com.zr.subclass.controller;

import com.zr.planclass.pojo.StatusVo;
import com.zr.subclass.pojo.SubClassAddVo;
import com.zr.subclass.pojo.SubClassSelectVo;
import com.zr.subclass.pojo.SubClassUpdateStatusVo;
import com.zr.subclass.pojo.SubClassUpdateVo;
import com.zr.subclass.service.SubClassService;
import com.zr.utils.ResultBuilderVo;
import com.zr.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2019/12/22.
 */
@RestController
@CrossOrigin//支持跨越
public class SubClassController {
    @Autowired
    private SubClassService planClassService;

    /*新增计划大类*/
    @PostMapping("subClass/add")
    public ResultVo add(@RequestBody @Valid SubClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.addPlanClass(planClassAddVo);
    }
    /*修改计划大类*/
    @PostMapping("subClass/update")
    public ResultVo update(@RequestBody @Valid SubClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updatePlanClass(planClassUpdateVo);
    }
    /*修改计划大类启用禁用状态*/
    @PostMapping("subClass/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid SubClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planClassService.updateStatus(planClassUpdateStatusVo);
    }
    /*查看*/
    @GetMapping("/subClass/see")
    public ResultVo see(Integer id){
        if (id==null){
            return ResultBuilderVo.error("查看时id不能为空");
        }
        return planClassService.see(id);
    }

    /*分页*/
    @PostMapping("subClass/queryPage")
    public ResultVo queryPage(@RequestBody SubClassSelectVo planClassSelectVo){

        return planClassService.queryPage(planClassSelectVo);
    }
    @GetMapping("metadata/getPullDownList1")
    public ResultVo queryStatus(){
        List<StatusVo> statusVoList = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel("禁用");
        statusVo.setValue(0);
        statusVoList.add(statusVo);
        StatusVo statusVo2 = new StatusVo();
        statusVo2.setLabel("启用");
        statusVo2.setValue(1);
        statusVoList.add(statusVo2);
        return ResultBuilderVo.success(statusVoList);
    }

}
